<template>
	<ge-page id="ge-page"  >
		<view class="form-list">
			<view class="cont-one">
				<view class="form-label">店铺名称</view>
				<view class="form-content">
					<input type="text" v-model="storeInfo.store_name" class="common-input" placeholder="请输入" />
				</view>
                <view class="form-label">联系电话</view>
				<view class="form-content">
					<input type="text" v-model="storeInfo.phone" class="common-input" placeholder="请输入" />
				</view>
				<view class="form-label">营业时间</view>
				<view class="form-content">
					<input type="text" v-model="storeInfo.business_time" class="common-input" placeholder="请输入" />
				</view>
				<view class="form-label">店铺图片</view>
				<view class="form-content">
					<uni-file-picker  v-model="shoplogo" :limit="1" @select='selectUpload' fileMediatype="image" mode="grid"
						@delete="onRemoveUpload" :image-styles="uploadListSty" />
				</view>
				 
				<view class="form-label">商品分类</view>
				<view class="form-content">
					<uni-data-checkbox multiple v-model="storeInfo.categoryIds" :localdata="category"></uni-data-checkbox>
				</view>

				<!-- <view class="form-label">商品编码</view>
				<view class="form-content">
					<input type="text" v-model="goodsInfo.goods_no" class="common-input" placeholder="请输入" />
				</view>、

				<view class="form-label">运费模板</view>
				<view class="form-content">
					<uni-data-checkbox v-model="goodsInfo.delivery_id" :localdata="deliveryList"></uni-data-checkbox>
				</view> -->

				<view class="form-label">店铺状态</view>
				<view class="form-content">
					<uni-data-checkbox v-model="storeInfo.is_online" :localdata="status"></uni-data-checkbox>
				</view>

				
			</view>
			<view class="cont-one">
							<view class="form-label"><view style="width: 150rpx;float: left;">折扣设置</view><view style="color: red;">(请填写1~10折)</view></view>  
							<template>
								<view class="form-content">
									<text style="width:210rpx">广电员工</text>
									<input style="width: 100rpx;" type="text"  v-model="discount"  class="common-input" placeholder="请输入" />
									<text>折</text>
								</view>
							</template>
			</view>
			<view class="cont-one">
				<view class="form-label">打包费用（按每份商品计算）</view>
				<view class="form-content">
					<input style="width: 100rpx;" type="number" v-model="storeInfo.express_price" class="common-input" placeholder="请输入" />元
				</view>
			</view>	
			<view class="cont-one">
							<view class="form-label">外卖时间设置</view>
							<template>
								<view class="form-content">
									<text style="width:210rpx">早上</text>
									<view class="form-content" style="background-color: #fff;">
										<uni-data-select style="width: 80px;" v-model="value_z1" :localdata="range_z1" @change="change" ></uni-data-select>——
										<uni-data-select style="width: 80px;" v-model="value_z2" :localdata="range_z2" @change="change" ></uni-data-select>
									</view>
								</view>
								<view class="form-content">
									<text style="width:210rpx">中午</text>
									<view class="form-content" style="background-color: #fff;">
										<uni-data-select style="width: 80px;" v-model="value_m1" :localdata="range_m1" @change="change" ></uni-data-select>——
										<uni-data-select style="width: 80px;" v-model="value_m2" :localdata="range_m2" @change="change" ></uni-data-select>
									</view>
								</view>
								<view class="form-content">
									<text style="width:210rpx">晚上</text>
									<view class="form-content" style="background-color: #fff;">
										<uni-data-select style="width: 80px;" v-model="value_x1" :localdata="range_x1" @change="change" ></uni-data-select>——
										<uni-data-select style="width: 80px;" v-model="value_x2" :localdata="range_x2" @change="change" ></uni-data-select>
									</view>
								</view>
							</template>
			</view>
			 

			<view class="cont-one">
				<view class="form-label">店铺介绍</view>
				<view class="form-content">
					<textarea type="text" v-model="storeInfo.describe" class="common-input" placeholder="请输入" />
				</view>
			</view>


	  
		</view>
		<view class="keep-button" @click="handleKeep">
			保存
		</view>
	</ge-page>
</template>

<script>
	import {
		storageKey,
		appConfig
	} from '@/constant/config';
 
	export default {
		props: {

		},
		data() {
			return {
				formData: {
					proTypeId: null,
				},
				uploadListSty: {
					width: 80,
					height: 80,
					border: {
						color: "#fff",
						width: 2,
						radius: '2px'
					}
				},
				category: [],
				storeInfo: {}, 
				status: [{
						text: '上架',
						value: 1
					},
					{
						text: '下架',
						value: 0
					},
				],
				shoplogo:[],
				discount:10,
				value_z1: 0,
				range_z1: [{"value": 0,"text": "不设置"},{"value": "5,0","text": "5:00"},{"value": "5,30","text": "5:30"},{"value": "6,0","text": "6:00"},
				{"value": "6,30","text": "6:30"},{"value": "7,0","text": "7:00"},{"value": "7,30","text": "7:30"},{"value": "8,0","text": "8:00"}],
				value_z2: 0,
				range_z2: [{"value": 0,"text": "不设置"},{"value": "6,0","text": "6:00"},{"value": "6,30","text": "6:30"},{"value": "7,0","text": "7:00"},
				{"value": "7,30","text": "7:30"},{"value": "8,0","text": "8:00"},{"value": "8,30","text": "8:30"},{"value": "9,0","text": "9:00"}],
				value_m1: 0,
				range_m1: [{"value": 0,"text": "不设置"},{"value": "8,0","text": "8:00"},{"value": "8,30","text": "8:30"},{"value": "9,0","text": "9:00"},{"value": "9,30","text": "9:30"},{"value": "10,0","text": "10:00"},
				{"value": "10,30","text": "10:30"},{"value": "11,0","text": "11:00"},{"value": "11,30","text": "11:30"}],
				value_m2: 0,
				range_m2: [{"value": 0,"text": "不设置"},{"value": "10,0","text": "10:00"},{"value": "10,30","text": "10:30"},{"value": "10,45","text": "10:45"},{"value": "11,0","text": "11:00"},
				{"value": "11,30","text": "11:30"},{"value": "12,0","text": "12:00"}],
				value_x1: 0,
				range_x1: [{"value": 0,"text": "不设置"},{"value": "14,0","text": "14:00"},{"value": "14,30","text": "14:30"},
				{"value": "15,0","text": "15:00"},{"value": "15,30","text": "15:30"},{"value": "16,0","text": "16:00"},{"value": "16,30","text": "16:30"},
				{"value": "17,0","text": "17:00"},{"value": "17,30","text": "17:30"},{"value": "18,0","text": "18:00"}],
				value_x2: 0,
				range_x2: [{"value": 0,"text": "不设置"},{"value": "16,0","text": "16:00"},{"value": "16,30","text": "16:30"},{"value": "17,0","text": "17:00"},
				{"value": "17,30","text": "17:30"},{"value": "17,45","text": "17:45"},{"value": "18,0","text": "18:00"},{"value": "18,30","text": "18:30"},{"value": "19,0","text": "19:00"},
				{"value": "19,30","text": "19:30"},{"value": "20,0","text": "20:00"},{"value": "20,30","text": "20:30"},{"value": "21,0","text": "21:00"}]
			}
		},
		onLoad() {
			this.getStoreInfo()
		},
		methods: {
			 
			getStoreInfo(){
				let app = this
				uni.api.storeInfo().then(res => {
					if (!res.err) {
						app.storeInfo = res.storeInfo
						app.discount = JSON.parse(res.storeInfo.grade.equity).discount
						if(res.storeInfo.storeDelivery.breakfast !='' && res.storeInfo.storeDelivery.breakfast.split(",").length == 4){
							app.value_z1 = String(res.storeInfo.storeDelivery.breakfast.split(",")[0]??'') + "," + String(res.storeInfo.storeDelivery.breakfast.split(",")[1]??'')
							app.value_z2 = String(res.storeInfo.storeDelivery.breakfast.split(",")[2]??'') + "," + String(res.storeInfo.storeDelivery.breakfast.split(",")[3]??'')
						} 
						if(res.storeInfo.storeDelivery.lunch !='' && res.storeInfo.storeDelivery.lunch.split(",").length == 4){
							app.value_m1 = String(res.storeInfo.storeDelivery.lunch.split(",")[0]??'') + "," + String(res.storeInfo.storeDelivery.lunch.split(",")[1]??'')
							app.value_m2 = String(res.storeInfo.storeDelivery.lunch.split(",")[2]??'') + "," + String(res.storeInfo.storeDelivery.lunch.split(",")[3]??'')
						}
						if(res.storeInfo.storeDelivery.dinner !='' && res.storeInfo.storeDelivery.dinner.split(",").length == 4){
							app.value_x1 = String(res.storeInfo.storeDelivery.dinner.split(",")[0]??'') + "," + String(res.storeInfo.storeDelivery.dinner.split(",")[1]??'')
							app.value_x2 = String(res.storeInfo.storeDelivery.dinner.split(",")[2]??'') + "," + String(res.storeInfo.storeDelivery.dinner.split(",")[3]??'')
						} 
						 
		                if (res.storeInfo && res.storeInfo.logoImage && typeof res.storeInfo.logoImage === 'object') {  
		                    res.storeInfo.logoImage.url = res.storeInfo.logoImage.preview_url; // 假设这是logo的URL  
		                }
						app.shoplogo.push(res.storeInfo.logoImage)
					}
				})
				uni.api.getGoodsCategory().then(res => {
					this.category = (res.list || []).map(d => ({
						text: d.name,
						value: d.category_id
					}));
				})
			}, 
			 
			// 选择图上传
			selectUpload(e) {
				const token = uni.getStorageSync(storageKey.token) || null;
				const tempFiles = e.tempFiles;
				let app = this
				tempFiles.forEach(d => {
					uni.showLoading({
						title: '加载中'
					})
					uni.uploadFile({
						url: `${appConfig.host}/store/upload/image`,
						name: 'iFile',
						header: {
							'Access-Token': token
						},
						filePath: d.path,
						formData: {
							groupId: 0
						},
						success: (res) => {
							const {
								data
							} = JSON.parse(res.data);
						 
							if (JSON.parse(res.data).status == 200) {
								data.fileInfo.url = data.fileInfo.external_url;
								data.fileInfo.name = data.fileInfo.file_name; 
								app.storeInfo.logo_image_id  = data.fileInfo.file_id
							}else{
								uni.showToast({
									title: JSON.parse(res.data).message,
									icon: 'error',
									duration: 2000 // 提示框的显示时间，单位毫秒，默认1500  
								});
							}
						},
						complete: () => {
							uni.hideLoading();
						}
					})
				})
			},
			// 删除上传图片
			onRemoveUpload(e) {
				this.storeInfo.goods_images = this.storeInfo.goods_images.filter((d, ii) => ii === e.index);
			},
			 
			  

			//保存
			handleKeep() {
				let app = this
				const storeInfo = app.storeInfo; 
				console.log(storeInfo) 
				  
				const formData = {
					...storeInfo,
					discount:app.discount,
					value_z1:app.value_z1,
					value_z2:app.value_z2,
					value_m1:app.value_m1,
					value_m2:app.value_m2,
					value_x1:app.value_x1,
					value_x2:app.value_x2,
				}
				console.log(11,formData)
				uni.api.updateStore({ form: formData}).then(res => {
					if(!res.err){
						uni.showToast({
						            title: '编辑成功',  
						            icon: 'success',  
						            duration: 2000 // 提示框的显示时间，单位毫秒，默认1500  
						}); 
						 // setTimeout(() => {  
						 //    // 跳转到目标页面  
						 //    //页面跳转到订单列表
						 //    window.location.href = '/storeadmin';  
						 // }, 1000);
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.form-list {
		padding: 30rpx;
		padding-bottom: 100rpx;
		box-sizing: border-box;

		.cont-one {
			background-color: #eee;
			border-radius: 10rpx;
			padding: 20rpx;
			box-sizing: border-box;
			margin-bottom: 20rpx;

			.sku-name {
				margin-bottom: 20rpx;
			}

			.form-label {
				margin-bottom: 15rpx;
			}

			.form-content {
				margin-bottom: 30rpx;
				display: flex;
				align-items: center;

				.common-input {
					width: 100%;
					border: 1rpx solid #ddd;
					padding: 10rpx;
					border-radius: 10rpx;
					background-color: #fff;
				}

				.delete-icon {
					margin-left: 20rpx;
				}
			}
		}
	}

	.keep-button {
		width: 300px;
		height: 70rpx;
		background-color: red;
		color: #fff;
		border-radius: 35rpx;
		font-size: 35rpx;
		font-weight: bold;
		display: flex;
		justify-content: center;
		align-items: center;
		position: fixed;
		left: 50%;
		bottom: 60rpx;
		transform: translate(-50%, 0);
	}

	.add-btn {
		padding: 5rpx 20rpx;
		min-width: 150rpx;
		text-align: center;
		margin-right: 10rpx;
		border: 1px solid #999;
		border-radius: 5rpx;
	}

	.specList {
		margin-bottom: 4px;
	}

	.valueList,
	.sku-items,
	.specList {
		padding: 8rpx;
		border: 1px solid #999;
	}

	.sku-list {
		.sku-items {
			margin-bottom: 8rpx;
		}

		.form-items {
			display: flex;

			&>view {
				flex: 1;

				&:first-child {
					margin-right: 4rpx;
				}

				&:last-child {
					margin-left: 4rpx;
				}
			}
		}
	}
</style>